//定义懒加载插件
//https://cn.vuejs.org/
import { useIntersectionObserver } from "@vueuse/core";
export const lazyPlugin = {
  install(app) {
    //懒加载指令逻辑
    app.directive("img-lazy", {
      mounted(el, binding) {
        //el:指令绑定的元素(img)
        //binding:binding.value 指令等于号后面绑定的表达式的值(图片url)
        // console.log(el, binding);
        const { stop } = useIntersectionObserver(el, ([{ isIntersecting }]) => {
          // isIntersecting 布尔值：代表当前监听区域是否进入视口区域
          console.log(isIntersecting);
          if (isIntersecting) {
            //引入视楼口区域
            el.src = binding.value;
            //关闭监听
            stop();
          }
        });
      },
    });
  },
};
